package com.mljr.example.base;

import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.ctakit.sdk.app.base.MyActivity;
import com.ctakit.sdk.exception.BusinessException;
import com.ctakit.sdk.util.JsonUtils;
import com.mljr.example.R;
import com.mljr.example.bean.MyLocaction;
import com.mljr.example.service.SystemService;
import com.mljr.example.ui.home.HomeActivity;
import com.mljr.example.util.AppUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lizhenmin on 8/2/16.
 */
public class BaseUtil {



    public static  void initBack(final MyActivity myActivity,String label){

        LinearLayout back = (LinearLayout) myActivity.findViewById(R.id.bar_back);
        if (back != null) {
            ((TextView) myActivity.findViewById(R.id.backLabel)).setText(label);
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!myActivity.goBack(v, false)) {
                        myActivity.getActivity().finish();
                    }
                }
            });
            back.setVisibility(View.VISIBLE);
        }
    }

    public static AMapLocationClient initLocation(AMapLocationListener listener) {
        //初始化定位
        AMapLocationClient mLocationClient = new AMapLocationClient(AppUtils.getContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(listener);
        //初始化定位参数
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        return mLocationClient;
    }


    public static  void  onLocationChanged(AMapLocation amapLocation,AMapLocationClient mLocationClient) {
        if (amapLocation != null) {
            if (amapLocation.getErrorCode() == 0) {
                MyLocaction locactionInfo = new MyLocaction();
                //定位成功回调信息，设置相关消息
                locactionInfo.setLocationtype(amapLocation.getLocationType());//获取当前定位结果来源，如网络定位结果，详见定位类型表
                locactionInfo.setLatitude(amapLocation.getLatitude());//获取纬度
                locactionInfo.setLongitude(amapLocation.getLongitude());//获取经度
                locactionInfo.setAccuracy(amapLocation.getAccuracy());//获取精度信息
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(amapLocation.getTime());
                df.format(date);//定位时间
                locactionInfo.setAddress(amapLocation.getAddress());//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
                locactionInfo.setCountry(amapLocation.getCountry());//国家信息
                locactionInfo.setProvince(amapLocation.getProvince());//省信息
                locactionInfo.setCity(amapLocation.getCity());//城市信息
                locactionInfo.setDistrict(amapLocation.getDistrict());//城区信息
                locactionInfo.setStreet(amapLocation.getStreet());//街道信息
                locactionInfo.setStreetnum(amapLocation.getStreetNum());//街道门牌号信息
                locactionInfo.setCitycode(amapLocation.getCityCode());//城市编码
                locactionInfo.setAdcode(amapLocation.getAdCode());//地区编码
                locactionInfo.setAoiname(amapLocation.getAoiName());//
                Global.setMapLocation(JsonUtils.toJson(locactionInfo));
                mLocationClient.stopLocation();//停止定位

            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                        + amapLocation.getErrorCode() + ", errInfo:"
                        + amapLocation.getErrorInfo());
            }
        }
    }


    public static boolean checkBussinssExcetion(final MyActivity activty, BusinessException[] ex) {
        if (ex[0].getCode() == 66058) {
            if (activty.getActivity() != null) {
                SystemService.clearData(false);//清除用户信息
                activty.showToastMsg("登录失败，请重新登录。");
            }
            activty.gotoActivity(HomeActivity.class);
            return true;
        }
        return false;
    }

}
